package bx.httpserver.router;

import akka.actor.typed.ActorRef;
import akka.actor.typed.ActorSystem;
import akka.actor.typed.Scheduler;
import akka.http.javadsl.marshallers.jackson.Jackson;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.Route;
import bx.InteractionEntity.CommonResponse;
import bx.InteractionEntity.JobWorkRequest;
import bx.httpserver.actor.JobManagerActor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

import static akka.http.javadsl.server.Directives.*;
import static bx.InteractionEntity.CommonResponse.SUCCESS_STATUS;

public class JobWorkerRoutes {
    //#user-routes-class
    private final static Logger log = LoggerFactory.getLogger(JobWorkerRoutes.class);
    private final ActorRef<JobManagerActor.JobManagerActorMsg> jobManagerActor;
    private final Duration askTimeout;
    private final Scheduler scheduler;

    public JobWorkerRoutes(ActorSystem<?> system, ActorRef<JobManagerActor.JobManagerActorMsg> jobManagerActor) {
        this.jobManagerActor = jobManagerActor;
        scheduler = system.scheduler();
        askTimeout = system.settings().config().getDuration("bx-job-worker.routes.ask-timeout");
    }


    public Route jobWorkerRoutes() {
        return concat(
                path("exec", () ->
                        post(() -> entity(
                                Jackson.unmarshaller(JobWorkRequest.class),
                                jobWorkRequest ->
                                        onSuccess(toWork(jobWorkRequest), performed -> {
                                            log.info("execute success: {}", jobWorkRequest);
                                            CommonResponse response = new CommonResponse();
                                            response.setStatus(SUCCESS_STATUS);
                                            return complete(StatusCodes.OK, response, Jackson.marshaller());
                                        })
                        ))));
    }

    private CompletionStage<Object> toWork(JobWorkRequest jobWorkRequest) {
        return CompletableFuture.completedFuture("ok");
    }


}
